<template>
	<!-- 陆亿版本 -->
	<view>
		<!-- 头部搜索框 -->
		<search :placeholder="$t('homepage.ProductionMonitor.tieNo')" />
		<!-- 查询结果列表 -->
		<zy-table :headList="headList" :dataList="dataList" @editConfirm="editConfirm" border>
		</zy-table>
		<bottomBtn fixed :leftText="$t('public.reset')" :rightText="$t('public.submit')"
			:leftDisabled="dataList.length>0?false:true" :rightDisabled="dataList.length>0?false:true"
			@rightBtn="confirmBtn" @leftBtn="resetBtn">
		</bottomBtn>
		<!-- 提交确认 -->
		<zy-popup ref="popup" :title="$t('page.SemiOutstorage')" type="dialog" @cancel="cancel" @confirm="submitBtn">
			{{$t('public.isSure')}}{{$t('page.SemiOutstorage')}}{{$t('public.question')}}
		</zy-popup>
	</view>
</template>
<script>
	import {
		packingBarcodeNum,
		outPackingType
	} from "@/api/homepage.js"
	export default {
		data() {
			return {
				headList: [{
					name: this.$t('homepage.WarehouseManage.barCodeTitle'),
					key: 'packingBarcode',
				}, {
					name: this.$t('public.num'),
					key: 'num',
					edit: true,
					type: 'number',
					sum: true
				}],
				dataList: [],
			}
		},
		onLoad() {
			var _this = this
			uni.$on('scancodedate', function(data) {
				_this.searchList(data.code)
			})
		},
		onUnload() {
			// 移除监听事件      
			uni.$off('scancodedate')
		},
		methods: {
			searchList(e) {
				packingBarcodeNum({
					packingBarcode: e,
				}).then((res) => {
					if (res.code == 0) {
						if (this.dataList.findIndex(item => item.packingBarcode === e) !== -1) {
							uni.showToast({
								title: this.$t('public.searchRepeat'),
								icon: "none",
								duration: 3000
							})
						} else {
							this.dataList.push({
								packingBarcode: res.data.packingBarcode,
								num: res.data.num,
								surplusNum: res.data.num,
							})
						}
					} else {
						uni.showToast({
							title: res.message,
							icon: "none",
							duration: 3000
						})
					}
				}).catch((err) => {
					uni.showToast({
						title: err.message,
						icon: "none",
						duration: 3000
					})
				});
			},
			//编辑出库数量
			editConfirm(val) {
				if (Number(val.item.num) > Number(this.dataList[val.index].surplusNum)) {
					this.dataList[val.index].num = this.dataList[val.index].surplusNum
					uni.showToast({
						title: this.$t('public.max') + this.dataList[val.index]
							.surplusNum,
						icon: "none",
						duration: 3000
					})
				}else if(Number(val.item.num)==0 || !Number(val.item.num)){
					this.dataList[val.index].num = this.dataList[val.index].surplusNum
					uni.showToast({
						title: this.$t('homepage.WarehouseManage.inputInvalid'),
						icon: "none",
						duration: 3000
					})
				}
			},
			// 重置
			resetBtn() {
				uni.redirectTo({
					url: "/pages/HomePage/WarehouseManage/ProductManagement/SemiOutstorage/index"
				})
			},
			//提交
			confirmBtn() {
				this.$refs.popup.open()
			},
			//弹窗取消
			cancel() {
				this.$refs.popup.close()
			},
			// 出库
			submitBtn: function() {
				let packingBarcode = ""
				let num = ""
				for (var i = 0; i < this.dataList.length; i++) {
					packingBarcode += this.dataList[i].packingBarcode + ","
					num += this.dataList[i].num + ","
				};
				outPackingType({
					packingBarcode: packingBarcode,
					num: num,
				}).then((res) => {
					uni.showToast({
						title: res.message,
						icon: "none",
						duration: 3000
					})
					if (res.code == 0) {
						uni.redirectTo({
							url: "/pages/HomePage/WarehouseManage/ProductManagement/SemiOutstorage/index"
						})
					} else {
						this.$refs.popup.close()
					}
				}).catch((res) => {
					this.$refs.popup.close()
					uni.showToast({
						title: res.message,
						icon: "none",
						duration: 3000
					})
				});
			},
		}
	}
</script>
<style lang="scss" scoped>
</style>